<?php

namespace app\v1\controller;

use think\App;
use think\Controller;
use think\Exception;
use think\exception\HttpResponseException;
use think\Response;

class BaseController extends Controller
{
    public $param;
    public $userId;
    public $m;
    public $c;
    public $a;

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    public function initialize()
    {
        parent::initialize();

        $request = request();
        $param = $request->param();
        $platform = isset($param['platform']) ? '_' . $param['platform'] : ''; //请求平台(mobile,ding)
        unset($param['platform']);
        $this->param = $param;

        $header = $request->header();
        if (!empty($header['authorization']) || !empty($header['Authorization'])) {
            $result = $this->checkToken($header['authorization'] ?? $header['Authorization']);
            if (!$result) {
                $response = errorReturn(50000, '登录失效');
                throw new HttpResponseException($response);
            }
            $this->userId = $result;
        }
        $this->m = strtolower($request->module());
        $this->c = strtolower($request->controller());
        $this->a = strtolower($request->action());
    }

    public function checkToken($authorization)
    {
        $user_id = think_ucenter_decrypt($authorization);
        if (!$user_id) {
            return false;
        }
        return $user_id;
    }
}